<?php

namespace App\Transformers;

use App\Models\Menu;
use League\Fractal\TransformerAbstract;

class MenuTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['parent', 'children'];

    public function transform(Menu $menu)
    {
        return $menu->attributesToArray();
    }

    public function includeChildren(Menu $menu)
    {
        if (!$menu->children) {
            return $this->null();
        }

        return $this->collection($menu->children, new MenuTransformer());
    }

    public function includeParent(Menu $menu)
    {
        if (!$menu->parent) {
            return $this->null();
        }

        return $this->collection($menu->parent, new MenuTransformer());
    }
}